//
//  DynamicSendView.m
//  MJApp
//
//  Created by wangliang on 15/5/28.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "DynamicSendView.h"
#import "MinePhotosCollectionViewCell.h"
#import "CamareModel.h"
#import "RSKImageCropViewController.h"

@interface DynamicSendView ()<UITextViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,HomeCellDelegate>
{
    BOOL isHidden;
}

@property (nonatomic, strong) UICollectionView *collectionView;
@property (assign, nonatomic) NSInteger allNum;
@property (nonatomic, strong) NSMutableArray *imageS;
@property (nonatomic, strong) UILabel *topLabel;


@end

@implementation DynamicSendView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if(self = [super initWithFrame:frame]) {
        
        self.frame = frame;
        [self UI];
    }
    return self;
}

- (NSMutableArray *)imageS {
    
    if (!_imageS) {
        _imageS = [NSMutableArray array];
    }
    return _imageS;
}

- (void)UI {
    
    isHidden = NO;
    self.allNum = 1;
    self.frame = [UIApplication sharedApplication].keyWindow.bounds;
    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 64)];
    topView.backgroundColor = Color_Back_Black;
    [self addSubview:topView];
    UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, self.frame.size.width, 30)];
    topLabel.text = @"发表图文";
    topLabel.textColor = [UIColor whiteColor];
    topLabel.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:topLabel];
    self.topLabel = topLabel;
    
    CGFloat high = 180;//图文高度
    high = 156;//文字高度
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.frame), high)];
    backView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [self addSubview:backView];
    self.backView = backView;
    [self addBackView:backView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notice:) name:@"changeImage" object:nil];
    
}

- (void)setHeadTitle:(NSString *)headTitle {
    
    _headTitle = headTitle;
    self.topLabel.text = headTitle;
}

- (void)notice:(NSNotification *)notification {
    
    UIImage *image = notification.userInfo[@"image"];
    [self.imageS addObject:image];
    self.allNum = self.imageS.count + 1;
    [self.collectionView reloadData];

}

- (void)addBackView:(UIView *)view {
    
    UIImage *cancelImage = [UIImage imageNamed:@"hd_09"];
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, cancelImage.size.width,cancelImage.size.height)];
    [cancelBtn setImage:cancelImage forState:UIControlStateNormal];
    [view addSubview:cancelBtn];
    cancelBtn.tag = 1;
    [cancelBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImage *okImage = [UIImage imageNamed:@"hd_12"];
    UIButton *okBtn = [[UIButton alloc] initWithFrame:CGRectMake(view.frame.size.width - okImage.size.width - 10, 5, okImage.size.width,okImage.size.height)];
    [okBtn setImage:okImage forState:UIControlStateNormal];
    [view addSubview:okBtn];
    okBtn.tag = 2;
    [okBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIView *textBackView = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(cancelBtn.frame) + 5, view.frame.size.width - 20, CGRectGetHeight(view.frame) - 80)];
    textBackView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    textBackView.layer.borderWidth = 0.5;
    textBackView.backgroundColor = [UIColor whiteColor];
    [view addSubview:textBackView];
    
    UIImage *image = [UIImage imageNamed:@"hd_19"];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, image.size.width, image.size.height)];
    imageView.image = image;
    [textBackView addSubview:imageView];
    
    CGFloat wide = CGRectGetWidth(textBackView.frame) - image.size.width - 5 - 5;
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5 + image.size.width, 0, wide, CGRectGetHeight(view.frame) - 80)];
    [textView becomeFirstResponder];
    textView.delegate = self;
    self.textView = textView;
    [textBackView addSubview:textView];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.itemSize = CGSizeMake(80, 80);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(textBackView.frame) + 5, CGRectGetWidth(textBackView.frame), 80)
                                             collectionViewLayout:flowLayout];
    self.collectionView.scrollEnabled = NO;
    self.collectionView.backgroundColor = Color_Back_Gray;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:
     [UINib nibWithNibName:@"MinePhotosCollectionViewCell" bundle:nil]
          forCellWithReuseIdentifier:@"MinePhotosCollectionViewCell"];
    [self.backView addSubview:self.collectionView];

}


- (void)buttonAction:(UIButton *)sender {
    
    self.hidden = YES;
    [self.textView resignFirstResponder];
    if (sender.tag == 2) {
        
        if ([self.delegate respondsToSelector:@selector(homeCell:withInfo:)]) {
            
            [self.delegate homeCell:self withInfo:@{@"status":@"send"}];
        }
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    
    if (isHidden) {
        
        self.hidden = YES;
    }
    isHidden = YES;
}

- (void)resetView:(BOOL)is {

    CGFloat high = is ? 190 : 156;//图文高度
    self.backView.frame = CGRectMake(0, 100, CGRectGetWidth(self.backView.frame), high);
    [self.textView becomeFirstResponder];
    self.collectionView.hidden = !is;
}


#pragma mark collectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
    
    return self.allNum;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MinePhotosCollectionViewCell *cell =
    [collectionView dequeueReusableCellWithReuseIdentifier:@"MinePhotosCollectionViewCell"
                                              forIndexPath:indexPath];
    if (indexPath.row == self.allNum - 1) {
        
        cell.status = NO;
        cell.infoDic = @{@"image":[UIImage imageNamed:@"grzx_11"]};
    }else {
        cell.status = YES;
        cell.infoDic = @{@"image":self.imageS[indexPath.row],@"index":@(indexPath.row)};
    }
    cell.delegate = self;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if ([self.delegate respondsToSelector:@selector(homeCell:withInfo:)]) {
        
        [self.delegate homeCell:self withInfo:@{@"status":@"image",@"index":@(indexPath.row)}];
    }
}


- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeImage" object:nil];
}

- (void)homeCell:(id)cell withInfo:(id)info {
    
    NSInteger index = [info[@"index"] integerValue];
    [self.imageS removeObjectAtIndex:index];
    self.allNum --;
    [self.collectionView reloadData];
}

@end
